package com.li.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.li.recycleradapter.HeaderFooterAdapter;
import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/8/9
 * Description:适用于SwipeRefreshLayout
 */

public class RefreshLayout {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private HeaderFooterAdapter mHeaderFooterAdapter;
    private RecyclerView mRecyclerView;

    private View mFooterView;


    private View mLoadingView;
    private View mFinishView;
    private OnRefreshListener mRefreshListener;
    public RefreshLayout(Context context,
                         SwipeRefreshLayout swipeRefreshLayout,
                         RecyclerView recyclerView,
                         HeaderFooterAdapter adapter) {

        mFooterView= LayoutInflater.from(context)
                .inflate(R.layout.layout_footer,null);
        mLoadingView=mFooterView.findViewById(R.id.view_loading);
        mFinishView=mFooterView.findViewById(R.id.view_finish);


        mSwipeRefreshLayout = swipeRefreshLayout;
        mRecyclerView = recyclerView;
//        mHeaderFooterAdapter=new HeaderFooterAdapter(adapter);
        mHeaderFooterAdapter=adapter;
        addFooter(mFooterView);


        mRecyclerView.setAdapter(mHeaderFooterAdapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshStart();
                if (mRefreshListener != null) {
                    mRefreshListener.onRefreshing();
                }
            }
        });
        mHeaderFooterAdapter.setOnEdgeListener(new HeaderFooterAdapter.OnEdgeListener() {
            @Override
            public void onBottom() {
                loadStart();
                if (mRefreshListener != null) {
                    mRefreshListener.onLoading();
                }
            }
            @Override
            public void onLoadAll() {
                loadAll();
            }
        });
    }

    public void addHeader(View view){
        mHeaderFooterAdapter.addHeaderView(view);
    }
    public void addFooter(View view){

        mHeaderFooterAdapter.addFooterView(view);
    }

    public void setRefreshListener(OnRefreshListener refreshListener) {
        mRefreshListener = refreshListener;
    }

    public interface OnRefreshListener{
        void onRefreshing();
        void onLoading();
    }

    public void loadStart(){
        mHeaderFooterAdapter.setLoading(true);
    }
    public void loadComplete(){
        mHeaderFooterAdapter.setLoading(false);
    }
    public void loadAll(){
        mHeaderFooterAdapter.setLoadAll(true);
        mFinishView.setVisibility(View.VISIBLE);
        mLoadingView.setVisibility(View.GONE);
    }

    /**
     * 刷新开始
     * 1.Adapter不可以加载
     */
    public void refreshStart(){
        mLoadingView.setVisibility(View.VISIBLE);
        mFinishView.setVisibility(View.GONE);
        mHeaderFooterAdapter.setCanLoad(false);
    }
    /**
     * 刷新完毕之后
     * 要设置Adapter状态
     * 1.Adapter可以加载
     */
    public void refreshComplete(){
        mHeaderFooterAdapter.setLoadAll(false);
        mHeaderFooterAdapter.setLoading(false);
        mSwipeRefreshLayout.setRefreshing(false);
        mHeaderFooterAdapter.setCanLoad(true);
    }
}
